<?php

namespace app\admin\model\site;

use app\common\exception\AdminApiException;
use think\facade\Db;
use think\Model;

/**
 * Group
 */
class Group extends Model
{
    // 表名
    protected $name = 'site_group';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;


    //增加删除事件
    //删除前事件
    protected static function onBeforeDelete($model)
    {
        if(Db::name('site')->where('site_group_id',$model->id)->count() > 0){
            throw new AdminApiException('该套餐下有站点，无法删除');
        }
    }



    public function setGroupRolesAttr($value, $data)
    {
        if(is_array($value)){
            return implode(',', $value);
        }
        return $value;
    }


    public function getGroupRolesAttr($value, $data)
    {
        if(!is_array($value)){
            return explode(',',$value);
        }
        return $value;
    }



}